﻿using log4net;
using StoreProject.BLL;
using StoreProject.BLL.SettingBLL;
using StoreProject.DAL;
using StoreProject.Entity;
using StoreProject.Entity.SettingEntity;
using StoreProject.Enum;
using StoreProject.Forms.Base;
using StoreProject.Forms.WorkingProcedure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreProject.Forms.SettingForms
{
    public partial class WorkingProcedureQueryForm : ConfigQueryForm
    {
        ILog _log = LogManager.GetLogger("WorkingProcedureQueryForm");
        WorkingProcedureBLL workingProcedureBLL = new WorkingProcedureBLL();
        public WorkingProcedureQueryForm()
        {
            InitializeComponent();
        }

        
        private void WorkingProcedureQueryForm_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            this.Query();
        }
        #region 顶部菜单重写继承方法

        public override void Add()
        {
            WorkingProcedureForm workingProcedureForm = new WorkingProcedureForm(FormState.ADD);
            workingProcedureForm.TopMost = true;
            workingProcedureForm.ShowDialog();
            workingProcedureForm.BringToFront();
            this.Query();
        }

        public override void Cancel()
        {

        }

        public override void Exit()
        {
            this.Close();
        }
        public override void OnUpdate()
        {
            ProcessFlow customerEntity = this._currentObj as ProcessFlow;
            if (CanOperator() && !string.IsNullOrEmpty(customerEntity.Id))
            {
                List<WorkingProcedureEntity> list = null;
                list = workingProcedureBLL.GetListByStatus(customerEntity.Id).ToList();
                
                WorkingProcedureForm workingProcedureForm = new WorkingProcedureForm(FormState.UPDATE, list);
                workingProcedureForm.TopMost = true;
                workingProcedureForm.ShowDialog();
                workingProcedureForm.BringToFront();
                this.Query();
            }
            else
            {
                MessageBox.Show("请选中要更新的行数据。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private bool CanOperator()
        {
            if (_currentObj == null)
                return false;
            return true;
        }

        public override void OnDelete()
        {
            ProcessFlow processFlow = this._currentObj as ProcessFlow;
            if (CanOperator() && !string.IsNullOrEmpty(processFlow.Id))
            {
                var processFlowList = workingProcedureBLL.GetListByStatus(processFlow.Id);
                bool res = true;
                foreach (var item in processFlowList)
                {
                    item.IsDeleted = true;
                    var flag = workingProcedureBLL.Update(item);
                    if(!flag)
                        res = false;
                }
                if (res)
                {
                    MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Query();
                }
                else
                {
                    MessageBox.Show("删除失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的行数据！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        public override void Query()
        {
            try
            {
                List<ProcessFlow> data = new List<ProcessFlow>();
                var groupList = workingProcedureBLL.GetListEx().GroupBy(x=>x.ProcessFlowId);
                foreach ( var group in groupList) 
                {
                    ProcessFlow processFlow = new ProcessFlow();
                    processFlow.Id = group.Key;
                    string temp = "";
                    foreach ( var item in group.ToList().OrderBy(x=>x.IndexNum))
                    {
                        temp += (item.IndexName + "+");
                    }
                    if (!string.IsNullOrEmpty(temp))
                        temp = temp.Remove(temp.Length - 1,1);
                    processFlow.ProcessFlowName = temp;
                    data.Add(processFlow);
                }
                if (data != null && data.Count > 0)
                {
                    dataGridView1.DataSource = data;
                }
                else
                    dataGridView1.DataSource = null;
            }
            catch (Exception ex)
            {
                _log.Error("Query", ex);
            }
        }
        #endregion

        #region 右键菜单相关方法
        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Add();
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.OnUpdate();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.OnDelete();
        }
        #endregion

        #region DataGridView相关方法

        private void uiDataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[e.RowIndex].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        private void uiDataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            if (dgv.CurrentRow != null)
            {
                this._currentObj = dgv.CurrentRow.DataBoundItem;
            }
            else
            {
                this._currentObj = null;
            }
        }

        #endregion
    }

    public class ProcessFlow
    {
        public string Id { get; set; }

        public string ProcessFlowName { get; set; }
    }
}
